package com.example.campus.controller.user;

import com.example.campus.pojo.dto.PwdUpdateDTO;
import com.example.campus.pojo.dto.UserInfoDTO;
import com.example.campus.pojo.dto.UserLoginDTO;
import com.example.campus.pojo.dto.UserRegisterDTO;
import com.example.campus.pojo.entity.User;
import com.example.campus.pojo.vo.UserInfoVO;
import com.example.campus.pojo.vo.UserLoginVO;
import com.example.campus.properties.JwtProperties;
import com.example.campus.result.Result;
import com.example.campus.service.UserService;
import com.example.campus.utils.JwtUtil;
import com.example.campus.utils.constant.JwtClaimsConstant;
import com.example.campus.utils.constant.MessageConstant;
import com.example.campus.utils.context.BaseContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController("userUserController")
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户端用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @ApiOperation("用户注册新账号")
    public Result register(@RequestBody @Validated UserRegisterDTO userRegisterDTO) {
        userService.register(userRegisterDTO);
        return Result.success(MessageConstant.REGISTER_SUCCESS);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody @Validated UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        return Result.success(userLoginVO, MessageConstant.LOGIN_SUCCESS);
    }

    @GetMapping("/info")
    @ApiOperation("获取当前登录用户信息")
    public Result<UserInfoVO> getUserInfo() {
        Long id = BaseContext.getCurrentId();
        UserInfoVO userInfoVO = userService.getUserInfo(id);
        return Result.success(userInfoVO);
    }

    @PutMapping("/update")
    @ApiOperation("更改当前登录用户信息")
    public Result updateUserInfo(@RequestBody @Validated UserInfoDTO userInfoDTO) {
        userService.updateUserInfo(userInfoDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @PutMapping("/updatePwd")
    @ApiOperation("更新已登录用户密码")
    public Result updatePassword(@RequestBody @Validated PwdUpdateDTO pwdUpdateDTO) {
        userService.updatePassword(pwdUpdateDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }
}
